package com.zhang.service;

import com.alibaba.fastjson.JSONObject;
import com.zhang.GeoLocation;
import com.zhang.anno.MyAnnotation;
import com.zhang.entity.UserRelaVO;
import com.zhang.mapper.MyMapper;
import com.zhang.threads.MyTask;
import com.zhang.util.RequestHolder;
import jdk.nashorn.internal.scripts.JS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Service
@Slf4j
public class MyServiceImpl implements MyService {
    @Autowired
    private MyMapper myMapper;
    @Override
    public void invite(String pid, String sid) {
        myMapper.insert(pid,sid);
    }

    @Override
    public Integer countSub(String pid) {
        List<UserRelaVO> userRelaVOS = new ArrayList<>();
//        userRelaVOS 把原list传进去，传的是这个list的地址
        getSub(userRelaVOS,pid);
        return userRelaVOS.size();
    }

    @Override
    public void getGeo() {
        List<Map<String,String>> arrayList = new ArrayList<>();
        List<Map<String,String>> list =  myMapper.selectOrg();
        for (Map<String, String> map : list) {
            String orgNo = map.get("orgNo");
            String orgName = map.get("orgName");
            String lat = map.get("lat");
            String lng = map.get("lng");
            try {
                String json = GeoLocation.getGeoLocation(lat, lng);
                JSONObject jsonObject = JSONObject.parseObject(json);
                JSONObject regeocode = jsonObject.getJSONObject("regeocode");
                JSONObject addressComponent = (JSONObject) regeocode.get("addressComponent");
                String city = (String) addressComponent.get("city");
                String citycode = (String) addressComponent.get("citycode");
                String adcode = (String) addressComponent.get("adcode");
                String district = (String) addressComponent.get("district");
                String province = (String) addressComponent.get("province");

                Map<String, String> hashMap = new HashMap<>();

                hashMap.put("cityName",city);
                hashMap.put("cityCode",citycode);
                hashMap.put("adCode",adcode);
                hashMap.put("district",district);
                hashMap.put("province",province);
                hashMap.put("orgNo",orgNo);
                hashMap.put("orgName",orgName);




                arrayList.add(hashMap);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        myMapper.batchGeo(arrayList);
    }

    @Override
    @MyAnnotation
    public String execMethod(String id) {
        HttpServletRequest request = RequestHolder.getRequest();
        log.info("service中的:{}",request.getRequestURI());
        return null;
    }

    private void getSub(List<UserRelaVO> userRelaVOS, String pid) {
      List<UserRelaVO> subList  =   myMapper.selectByPid(pid);

      if (CollectionUtils.isEmpty(subList)) return ;//递归出口
//        把下级加入到list
        userRelaVOS.addAll(subList);
        //继续循环每个下级  拿到下级的下级
        for (UserRelaVO userRelaVO : subList) {
            getSub(userRelaVOS,userRelaVO.getSid());
        }
    }


}
